package com.lemo.smart.helper;

import com.lemo.smart.util.ReflectionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Bean助手类
 * Created by 罗选通 on 2017/12/22.
 */
public class BeanHelper {

  /**
   * 定义bean映射(用于存放bean和bean实例的映射关系)
   */
  private static final Map<Class<?>, Object> BEAN_MAP = new HashMap();

  static {
    Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
    for (Class<?> cls : beanClassSet) {
      Object o = ReflectionUtil.newInstance(cls);
      BEAN_MAP.put(cls, o);
    }
  }

  /**
   * 获取bean映射
   */
  public static Map<Class<?>, Object> getBeanMap() {
    return BEAN_MAP;
  }

  /**
   * 获取实例Bean
   */
  public static Object getBean(Class<?> cls) {
    if (!BEAN_MAP.containsKey(cls)) {
      throw new RuntimeException("can not get bean class" + cls);
    }
    return BEAN_MAP.get(cls);
  }

  /**
   * 设置bean的实例
   */
  public static void setBean(Class<?> cls, Object obj) {
    BEAN_MAP.put(cls, obj);
  }


}
